package com.simple.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.simple.entity.CodeResult;
import com.simple.entity.StatusCode;
import com.simple.entity.SysMenu;
import com.simple.entity.vo.SysMenuVo;
import com.simple.service.SysMenuService;
import com.simple.utils.TableUtils;

/**
 * 栏目管理
 */
@SuppressWarnings("unused")
@Controller
@RequestMapping("/user")
public class SysMenuController {
	
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(SysMenuController.class);

    @Autowired
    SysMenuService sysMenuService;

    @RequestMapping("/sysMenu.html")
    public String sysMenu(){
        return "sysMenu";
    }

    //表格数据
    @RequiresPermissions("sysMenu:view")
    @RequestMapping(value="/sysMenu/list.html")
    @ResponseBody
    public JSONObject tableSysMenu(){
        List<SysMenu> list=sysMenuService.findAll();
        logger.info("json--"+TableUtils.tableResult(list));
        return TableUtils.tableResult(list);
    }
    
    //编辑页面
    @RequiresPermissions(value={"sysMenu:edit","sysMenu:view"},logical = Logical.OR)
    @RequestMapping(value="/sysMenu/edit.html")
    public String editMenu(HttpServletRequest request,Model model) {
    	String id=request.getParameter("id");
		if(StringUtils.isNotBlank(id)) {
			SysMenuVo sysMenuVo=sysMenuService.findByMenuIdVo(Integer.valueOf(id));
			model.addAttribute("sysMenu", sysMenuVo);
		}
    	return "sysMenuEdit";
    }
    
	//获取栏目树
	@RequestMapping(value="/sysMenu/getMenuTree.html")
	@ResponseBody
	public Object getMenuTree(@RequestParam("startLevel")Integer startLevel,@RequestParam("endLevel")Integer endLevel) {
		List<HashMap<String, Object>> result=new ArrayList<HashMap<String, Object>>();
		//显示1-2级内容
		List<Object> list=sysMenuService.findMenuTree(startLevel,endLevel);
		JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
		for (int i = 0; i < array.size(); i++) {
			HashMap<String, Object> map = new HashMap<>();
			JSONObject job = array.getJSONObject(i);
			map.put("id", job.get("id"));
			map.put("title", job.get("title"));
			map.put("menuLevel",job.get("menuLevel"));
			map.put("spread", true);  
			JSONArray childArray=(JSONArray) job.get("children");
			map.put("children", childArray);
			result.add(map);
		}
		return result;
	}
	
	//添加
	@RequiresPermissions("sysMenu:edit")
	@RequestMapping("/sysMenu/add.html")
	@ResponseBody
	public CodeResult addMenu(SysMenu sysMenu,Model model) {
		sysMenuService.saveAndFlush(sysMenu);
		return CodeResult.success();
	}
	//删除
	@RequiresPermissions("sysMenu:edit")
    @RequestMapping("/sysMenu/delete/{id}.html")
    @ResponseBody
    public CodeResult deleteMenu(@RequestParam("id")Integer id) {
    	logger.info("删除栏目id="+id);
    	sysMenuService.deleteById(id);
    	return CodeResult.success(StatusCode.DELETE_SUCCESS);
    }
    
}
